﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using RoadTeam.Enums;
using System.Data;

namespace RoadTeam.Pub
{
   public class SyncTable
    {
       /// <summary>
       /// 同步DataTable算法
       /// </summary>
       /// <param name="dt">原始Datatable</param>
       /// <param name="Syncdt">被同步的DataTable</param>
       /// <param name="Rtidentity">原始关联列(不被同步)</param>
        /// <param name="idtntity">被同步表的关联列(不被同步)</param>
       /// <param name="EnmunType">枚举值</param>
       public static void SyncDataTable(DataTable dt, DataTable Syncdt, String Rtidentity, String idtntity, String EnmunType)
        {
            Dictionary<String, ColumnInfo> Dict = GetDict(EnmunType);
            Rtidentity = Rtidentity.ToLower();
            idtntity = idtntity.ToLower();
            DataRow[] drs;
            DataRow drs_Temp;
            String RtColumnName = String.Empty;
            String ColumnName = String.Empty;

            List<String> LiRtColumnName = new List<string>();
            List<string> LiColumnName = new List<string>();

            foreach (DataColumn col in dt.Columns)
                LiRtColumnName.Add(col.ColumnName.ToLower());
            foreach (DataColumn col in Syncdt.Columns)
                LiColumnName.Add(col.ColumnName.ToLower());
            foreach (DataRow dr in dt.Rows)
            {
                drs = Syncdt.Select(idtntity + "='" + dr[Rtidentity] + "'");
                if (drs.Count() == 0) continue;
                foreach (KeyValuePair<String, ColumnInfo> DictInfo in Dict)
                {
                    RtColumnName = DictInfo.Key.ToLower();
                    ColumnName = DictInfo.Value.ColumnName.ToLower();
                    //对比所有对应的字段,如果没有相同值
                    if (RtColumnName == Rtidentity || ColumnName == idtntity) continue;
                    drs_Temp = drs[0];
                    if (!LiRtColumnName.Contains(RtColumnName) || !LiColumnName.Contains(ColumnName)) continue;
                    if (drs_Temp[ColumnName] == dr[RtColumnName]) continue;

                    if (!String.IsNullOrEmpty(DictInfo.Value.ReflecClass) && !String.IsNullOrEmpty(DictInfo.Value.ReflecFunctionName))
                    {
                        Assembly assembly = Assembly.LoadFrom(System.Web.HttpContext.Current.Server.MapPath("~/Bin/" + GlobalConfig.RtSystemPath + ".dll"));
                        Type[] AllClass = assembly.GetTypes();
                        foreach (Type TiClass in AllClass)
                        {
                            if (TiClass.Name == DictInfo.Value.ReflecClass)
                            {
                                object Obj = Activator.CreateInstance(TiClass);
                                MethodInfo Mi = TiClass.GetMethods().Where(th => th.Name == DictInfo.Value.ReflecFunctionName).FirstOrDefault();
                                drs_Temp[ColumnName] = Mi.Invoke(Obj, new object[] { dr[RtColumnName].ToString() });
                            }
                        }
                    }
                    else
                        drs_Temp[ColumnName] = dr[RtColumnName];
                }
            }
        }
        public static Dictionary<String, ColumnInfo> GetDict(String EnumType)
        {
            Assembly assemly = Assembly.LoadFrom(System.Web.HttpContext.Current.Server.MapPath("~/Bin/RoadTeam.Enums.dll"));
            String ClassName = (GlobalConfig.RtStemType.ToUpper() == "CS") ? "Cs" + EnumType : "Bs" + EnumType;

            Type[] T = assemly.GetTypes();
            object ReturnObject = null;

            foreach (Type t in T)
            {
                if (t.Name == ClassName)
                {
                    object o = Activator.CreateInstance(t);
                    if (o == null) break;
                    foreach (PropertyInfo pi in t.GetProperties())
                    {
                        ReturnObject = pi.GetValue(o, null); break;
                    }
                    if (ReturnObject != null) break;
                }
            }
            if (ReturnObject != null)
                return (Dictionary<String, ColumnInfo>)ReturnObject;

            return null;
        }
    }
}
